<?php


/**
 * @user magein
 * @date 2024/1/18 17:30
 */

namespace seecms\develop\lib;

use magein\utils\Variable;


class Develop
{

    /**
     * 列格式化
     * @param $columns
     * @param string $sp
     * @return string
     */
    public function columnFormatter($columns, string $sp = ''): string
    {
        $formatter = '';
        foreach ($columns as $item) {
            $formatter .= "$sp'{$item}',\n";
        }
        return $formatter;
    }

    /**
     * @param $dictionary
     * @param $table_name
     * @return array
     */
    public function columnComment($dictionary, $table_name): array
    {
        $zh_cn = "//zh-cn\n '$table_name' => [ \n";
        foreach ($dictionary as $key => $item) {
            $zh_cn .= "    '$key' => '{$item}',\n";
        }
        $zh_cn .= '],';

        $en_us = "//en-us\n '$table_name' => [ \n";
        foreach ($dictionary as $key => $item) {
            $value = preg_replace('/_/', ' ', $key);
            $value = ucfirst($value);
            $en_us .= "    '$key' => '{$value}',\n";
        }
        $en_us .= ']';

        return compact('zh_cn', 'en_us');
    }

    /**
     * @param array $attrs
     * @return string
     */
    public function columnToAttrs(array $attrs): string
    {
        $property = '/**';
        $property .= "\n";

        foreach ($attrs as $attr) {
            $field = $attr['Field'];
            $type = $attr['Type'];
            if (in_array($field, ['id', 'created_at', 'updated_at', 'deleted_at'])) {
                continue;
            }
            if (preg_match('/int/', $type)) {
                $type = 'integer';
            } elseif (preg_match('/decimal/', $type)) {
                $type = 'float';
            } else {
                $type = 'string';
            }
            $property .= " * @property $type $" . $field;
            $property .= "\n";
        }
        $property = trim($property, "\n");
        $property .= "\n */";

        return $property;
    }

    /**
     * @param $columns
     * @param $table_name
     * @return string
     */
    public function pageBuilder($columns, $table_name): string
    {
        $columns = $this->columnFormatter($columns, '            ');
        $columns = trim($columns, "\n");
        $columns = trim($columns);

        $controls = str_replace("'created_at',", '', $columns);
        $controls = trim($controls, "\n");
        $controls = trim($controls);

        $class_name = Variable::pascal($table_name);
        $dictionary = Variable::underline($table_name);
        return <<<EOF
<?php

namespace app\admin\controller;

use seecms\controller\Resource;

class {$class_name} extends Resource
{
    protected \$table_name = '$table_name';

    protected \$dictionary_name = '$dictionary';
    
     protected function pageTitle(): string
    {
        return __('$table_name.page_title');
    }
    
    public function query(): array
    {
        return [
            $columns
        ];
    }
    
    public function columns(): array
    {
        return [
            $columns
        ];
    }
    
    public function controls(): array
    {
        return [
            $controls
        ];
    }
}
EOF;
    }
}